package com.baidu.health.error;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.io.IOException;

@Slf4j
//@ControllerAdvice
public class ExceptionController {
    @ExceptionHandler(Exception.class)
    public ModelAndView customException(Exception ex) {
        ex.printStackTrace();
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", ex.getMessage());
        mv.setViewName("page_error");
        return mv;
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public String handle404(Exception ex, Model model) {
        ex.printStackTrace();
        model.addAttribute("message", "您访问的页面不存在！");
        return "page_error";
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(HttpServerErrorException.InternalServerError.class)
    public String handle500(Exception ex, Model model) {
        ex.printStackTrace();
        model.addAttribute("message", "服务器内部错误！");
        return "page_error";
    }

    //运行时异常
    @ExceptionHandler(RuntimeException.class)
    public String runtimeExceptionHandler(RuntimeException ex, Model model) {
        ex.printStackTrace();
        model.addAttribute("message", "运行时发生错误！");
        return "page_error";
    }

    //空指针异常
    @ExceptionHandler(NullPointerException.class)
    public String nullPointerExceptionHandler(NullPointerException ex, Model model) {
        ex.printStackTrace();
        model.addAttribute("message", "空指针错误！");
        return "page_error";
    }

    //类型转换异常
    @ExceptionHandler(ClassCastException.class)
    public String classCastExceptionHandler(ClassCastException ex, Model model) {
        ex.printStackTrace();
        model.addAttribute("message", "类型转换错误！");
        return "page_error";
    }

    //IO异常
    @ExceptionHandler(IOException.class)
    public String iOExceptionHandler(IOException ex, Model model) {
        ex.printStackTrace();
        model.addAttribute("message", "IO错误！");
        return "page_error";
    }

    //未知方法异常
    @ExceptionHandler(NoSuchMethodException.class)
    public String noSuchMethodExceptionHandler(NoSuchMethodException ex, Model model) {
        ex.printStackTrace();
        model.addAttribute("message", "未知方法错误！");
        return "page_error";
    }

    //数组越界异常
    @ExceptionHandler(IndexOutOfBoundsException.class)
    public String indexOutOfBoundsExceptionHandler(IndexOutOfBoundsException ex, Model model) {
        ex.printStackTrace();
        model.addAttribute("message", "数组越界错误！");
        return "page_error";
    }
}